package com.xebest.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {
	public static String getClientIpAddress(HttpServletRequest request) {
		String ipAddress = request.getHeader("x-forwarded-for");
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
			ipAddress = request.getRemoteAddr();
			if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				}
				ipAddress = inet.getHostAddress();
			}
		}
		if (ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(",") > 0) {
			ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
		}
		return ipAddress;
	}
	/*
     * 获取核心浏览器名称
     */
	public static String getClientBrower(HttpServletRequest request){
		String info = request.getHeader("user-agent");
		String str = "未知";
		if(info.indexOf("MSIE") != -1){
			str = "IE"; //微软IE
		}else if(info.indexOf("Firefox") != -1){
			str = "Firefox"; //火狐
		}else if(info.indexOf("Chrome") != -1){
			str = "Chrome"; //Google
		}else if(info.indexOf("Opera") != -1){
			str = "Opera"; //Opera
		}
		return str;
	}
}
